﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace thinger.VisionControlLib
{
    public partial class UArrow : UserControl
    {
        public UArrow()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.SizeChanged += UArrow_SizeChanged;
            this.Size = new Size(100, 50);
        }

        private void UArrow_SizeChanged(object sender, EventArgs e)
        {
            ResetPath();
        }

        private Color arrowColor = Color.Red;
        [Description("箭头颜色"), Category("自定义")]
        public Color ArrowColor
        {
            get { return arrowColor; }
            set
            {
                arrowColor = value;
                Invalidate();
            }
        }

        private Color borderColor = Color.Gray;

        [Description("箭头的边框颜色"), Category("自定义")]
        public Color BorderColor
        {
            get { return borderColor; }
            set
            {
                borderColor = value;
                Invalidate();
            }
        }

        private bool isBorder;
        //是否有边框
        public bool IsBorder
        {
            get { return isBorder; }
            set
            {
                isBorder = value;
                Invalidate();
            }
        }


        private ArrowDirection direction = ArrowDirection.Left;
        [Description("箭头方向"), Category("自定义")]
        public ArrowDirection Direction
        {
            get { return direction; }
            set
            {
                direction = value;
                ResetPath();
                Invalidate();
            }
        }

        //绘制路径
        GraphicsPath m_path;

        //重置箭头形状路径
        private void ResetPath()
        {
            Point[] ps = null;
            switch (direction)
            {
                case ArrowDirection.Left:
                    ps = new Point[]
                    {
                        new Point(0,this.Height/2),
                        new Point((Width-1)/3,0),
                        new Point((Width-1)/3,Height/3),
                        new Point((Width-1),Height/3),
                        new Point((Width-1),Height-Height/3),
                        new Point((Width-1)/3,Height-Height/3),
                        new Point((Width-1)/3,Height),
                        new Point(0,this.Height/2)
                    };
                    break;
                case ArrowDirection.Right:
                    ps = new Point[]
                    {
                        new Point(0,this.Height/3),
                        new Point(this.Width-(Width-1)/3,this.Height/3),
                        new Point(this.Width-(Width-1)/3,0),
                        new Point(this.Width-1,this.Height/2),
                        new Point(this.Width-(Width-1)/3,this.Height),
                        new Point(this.Width-(Width-1)/3,this.Height-this.Height/3),
                        new Point(0,this.Height-this.Height/3),
                        new Point(0,this.Height/3)
                    };
                    break;
                case ArrowDirection.Top:
                    ps = new Point[]
                    {
                       new Point(this.Width/2,0),
                       new Point(this.Width,(Height-1)/3),
                       new Point(this.Width-this.Width/3,(Height-1)/3),
                       new Point(this.Width-this.Width/3,this.Height-1),
                       new Point(this.Width/3,this.Height-1),
                       new Point(this.Width/3,(Height-1)/3),
                       new Point(0,(Height-1)/3),
                       new Point(this.Width/2,0),
                    };
                    break;
                case ArrowDirection.Bottom:
                    ps = new Point[]
                    {
                       new Point(this.Width-this.Width/3,0),
                       new Point(this.Width-this.Width/3,this.Height-(Height-1)/3),
                       new Point(this.Width,this.Height-(Height-1)/3),
                       new Point(this.Width/2,this.Height-1),
                       new Point(0,this.Height-(Height-1)/3),
                       new Point(this.Width/3,this.Height-(Height-1)/3),
                       new Point(this.Width/3,0),
                       new Point(this.Width-this.Width/3,0),
                    };
                    break;
            }
            m_path = new GraphicsPath();
            m_path.AddLines(ps);
            m_path.CloseAllFigures();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            var g = e.Graphics;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.FillPath(new SolidBrush(ArrowColor), m_path);//填充箭头
            if (isBorder)
            {
                g.DrawPath(new Pen(new SolidBrush(borderColor)), m_path);
            }
        }
    }

    /// <summary>
    /// 剪头方向
    /// </summary>
    public enum ArrowDirection
    {
        /// <summary>
        ///左
        /// </summary>
        Left,
        /// <summary>
        /// 右
        /// </summary>
        Right,
        /// <summary>
        /// 上
        /// </summary>
        Top,
        /// <summary>
        /// 下
        /// </summary>
        Bottom


    }
}
